// @ts-nocheck
/**
 * @Date: 2022-04-07 19:09:49
 * @LastEditors: sailornpg
 * @LastEditTime: 2022-04-21 13:28:56
 * @FilePath: \weibo-api\middleware\check.js
 * @Description: 检查中间件
 */
const { getUserInfo } = require("../controller/userCtl");
const { SuccessModel, ErrorModel } = require("../ResModel/index");
const { Blog, User, Topic, Like } = require("../model/index");
//检查用户是否存在(登陆注册都用)
const checkUserExist = async (ctx, next) => {
  const body = ctx.request.body;
  if (body.type == "login") {
    const res = await getUserInfo(body.userName, body.password);
    // console.log("查询结果", res);
    if (!res) {
      ctx.body = new ErrorModel(409, "账号或者密码错误!");
    } else {
      await next();
    }
  } else if (body.type == "register") {
    const res = await getUserInfo(body.userName);
    if (!res) {
      await next();
    } else {
      ctx.body = new ErrorModel(409, "用户名已经存在!");
    }
  }
};
//检查博客是不是自己的
const checkBlogIsOwn = async (ctx, next) => {
  const { id } = ctx.params; //博客id
  const { id: userId } = ctx.session.userInfo;
  // console.log(id, userId);
  const userWhereOpt = {
    id: userId,
  };
  const res = await Blog.findAndCountAll({
    order: [["id", "desc"]],
    include: [
      {
        model: User,
        attributes: ["id"],
        where: userWhereOpt,
      },
    ],
  });
  // res.count 总数 res.rows查询结果数组
  // console.log(res);
  let blogList = res.rows.map((row) => row.dataValues);
  blogList = blogList.map((blogItem) => {
    blogItem.imageUrl = JSON.parse(blogItem.imageUrl);
    const user = blogItem.user.dataValues;
    blogItem.user = user;
    return blogItem;
  });
  let isOwnFlag = blogList.some((item) => {
    return id == item.id && userId == item.userId;
  });
  // console.log(isOwnFlag);
  if (isOwnFlag) {
    await next();
  } else {
    ctx.body = new ErrorModel(403, "只能删除自己的博客！");
  }
};
//检查是不是自己的评论
const checkCommentOwn = async (ctx, next) => {
  const { commentId } = ctx.params;
  const { id } = ctx.session.userInfo;
  // console.log("评论id是", parseInt(commentId), "评论用户id", id);
  const TopicWhereOpt = { id: parseInt(commentId), userId: parseInt(id) };
  let res = await Topic.findAll({
    where: TopicWhereOpt,
  });
  let topicList = res.map((row) => row.dataValues);
  // console.log(topicList);
  if (topicList.length > 0) {
    await next();
  } else {
    ctx.body = new ErrorModel(403, "只能删除自己的评论");
  }
};
//检查是否点赞过该博客
const checkLikeExist = async (ctx, next) => {
  const { id: userId } = ctx.session.userInfo;
  const { blogId } = ctx.params;
  let res = await Like.findOne({
    where: { userId, blogId, like_status: true },
  });
  if (res !== null) {
    ctx.body = new ErrorModel(409, "您已点赞过该博客");
  } else {
    await next();
  }
};
//检查关注的用户是不是自己
const followObjectIsMe = async (ctx, next) => {
  const { id: userId } = ctx.session.userInfo;
  const { id: followerId } = ctx.params;
  if (userId == followerId) {
    ctx.body = new ErrorModel(403, "自己不能关注自己");
  } else {
    await next();
  }
};
module.exports = {
  checkUserExist,
  checkBlogIsOwn,
  checkCommentOwn,
  checkLikeExist,
  followObjectIsMe,
};
